import { _decorator, Component, geometry, Node, PhysicsSystem } from 'cc';
import { PlayerController } from './PlayerController';
const { ccclass, property } = _decorator;

@ccclass('CameraDistance')
export class CameraDistance extends Component {

    start() {

    }

    update(deltaTime: number) {
        this.Check();
    }

    Check() {
        if (PlayerController.Instance.isParachute) return;
        let pos = this.node.parent.getWorldPosition();
        let forward = this.node.parent.forward.clone().negative();
        let ray = new geometry.Ray;
        geometry.Ray.fromPoints(ray, pos, forward);
        let dis = this.node.getPosition().z;
        if (PhysicsSystem.instance.raycastClosest(ray, 1 << 0, 5, false)) {
            const result = PhysicsSystem.instance.raycastClosestResult;
            dis = result.distance;
        }
        else if (this.node.getPosition().z < 5) dis = 5;
        this.node.setPosition(0.5, 0, dis);
    }

}


